package wiki.hitime.demo.test.bean.dto;

import lombok.Data;
import lombok.ToString;
import wiki.hitime.demo.test.enums.ResponseCodeEnum;

import java.io.Serializable;

/**
 * @author
 * @description:
 * @date: 2019-04-28 21:32
 */
@ToString
@Data
public class ResultDTO<T> implements Serializable {

    private static final long serialVersionUID = 4864531336362245509L;
    private String code;

    private String message;

    private T data;

    public boolean hasError() {
        return !ResponseCodeEnum.SUCCESS.getCode().equals(code);
    }

    public static <T> ResultDTO<T> success() {
        return success(ResponseCodeEnum.SUCCESS.getDes(), null);
    }

    public static <T> ResultDTO<T> success(T data) {
        return success(ResponseCodeEnum.SUCCESS.getDes(), data);
    }

    public static <T> ResultDTO<T> success(String message, T data) {
        ResultDTO<T> resp = new ResultDTO();
        resp.setCode(ResponseCodeEnum.SUCCESS.getCode());
        resp.setMessage(message);
        if (data != null) {
            resp.setData(data);
        }
        return resp;
    }

    public static <T> ResultDTO<T> fail() {
        return fail(ResponseCodeEnum.FAIL.getDes());
    }


    public static <T> ResultDTO<T> fail(String message) {
        return fail(message, null);
    }

    public static <T> ResultDTO<T> fail(String message, T data) {
        ResultDTO<T> resp = new ResultDTO();
        resp.setCode(ResponseCodeEnum.FAIL.getCode());
        resp.setMessage(message);
        if (data != null) {
            resp.setData(data);
        }
        return resp;
    }


    public static <T> ResultDTO<T> response(ResponseCodeEnum responseCodeEnum) {
        return response(responseCodeEnum.getCode(), responseCodeEnum.getDes(), null);
    }
    public static <T> ResultDTO<T> response(ResponseCodeEnum responseCodeEnum, T data) {
        return response(responseCodeEnum.getCode(), responseCodeEnum.getDes(), data);
    }

    public static <T> ResultDTO<T> response(String code, String message) {
        return response(code, message, null);
    }


    public static <T> ResultDTO<T> response(String code, String message, T data) {
        ResultDTO<T> resp = new ResultDTO();
        resp.setCode(code);
        resp.setMessage(message);
        if (data != null) {
            resp.setData(data);
        }
        return resp;
    }
}
